/**
 * Created by xuan on 15/12/10.
 */
define(function (require) {
    'use strict';
    var angular = require('angular');
    var _ = require('underscore');
    angular.module('cstAssignApp.directives', [])
        .directive('tabs', function () {
            return {
                // require: '',
                // restrict : '',
                // transclude: true,
                replace: true,
                scope: {
                    tabs: '=',
                    curr: '=tabsCurr'
                },
                link: function (scope) {
                    var tabs = scope.tabs;
                    scope.change = function (tab) {
                        scope.curr = tab;
                    };
                    // curr is null && init first
                    if (!scope.curr && angular.isArray(tabs)) {
                        scope.curr = tabs[0];
                    }
                },
                template: '<ul><li ng-repeat="tab in tabs" ng-class="{\'tab-on\':(curr === tab)}" ng-click="change(tab)"><a ng-href="{{tab.href}}">{{tab.text}}</a></li></ul>'
                // templateUrl: ''
            };
        })
        .directive('customTelValid', ['$q', 'cstAssignService', function ($q, cstAssignService) {
            return {
                require: 'ngModel',
                // restrict : '',
                // transclude: true,
                scope: {
                    customTelValid: '='
                },
                link: function (scope, element, attrs, ctrl) {
                    // 手机: 1, 家庭: 2 , 公司: 3, 传真: 4
                    var phoneReg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$/;//手机
                    var seatNoReg = /^\d{3}-\d{8}|\d{4}-\d{7,8}$/;  //座机正则
                    ctrl.$validators.pattern = function (modelValue, viewValue) {
                        var value = modelValue || viewValue;
                        if (scope.customTelValid.comms_type === '1') {
                            return phoneReg.test(value) || seatNoReg.test(value);
                        } else {
                            return seatNoReg.test(value);
                        }
                    };
                    ctrl.$asyncValidators.unique = function (modelValue, viewValue) {
                        var value = modelValue || viewValue;
                        // Lookup user by username
                        if (value === scope.customTelValid.comms_no_pre) {
                            var deferred = $q.defer();
                            deferred.resolve();
                            return deferred.promise;
                        }
                        return cstAssignService.checkTelIsExist({
                            cst_id: scope.customTelValid.cst_id,
                            cst_guid: scope.customTelValid.cst_guid,
                            tel: value
                        }).then(function (data) {
                            if (data) {
                                return $q.reject('电话号码已存在');
                            }
                            return true;
                        }, function () {
                            return true;
                        });
                    };
                }
                // template : '',
                // templateUrl: ''
            };
        }])
        .directive('dynamicMessages', ['$compile', function ($compile) {
            return {
                // require: '',
                // restrict : '',
                // transclude: true,
                link: function (scope, element) {
                    var name = 'comms_' + scope.comms.comms_type;
                    var tpl = '<div ng-messages="customerForm.' + name + '.$error" ng-show="customerForm.$submitted || customerForm.' + name + '.$touched">' +
                        '<p class="c-red" ng-message="required">{{comms.comms_type_text}}不能为空</p>' +
                        '<p class="c-red" ng-message="pattern">格式不正确</p>' +
                        '<p class="c-red" ng-message="unique">号码已存在</p>' +
                        '</div>';
                    element.append($compile(tpl)(scope));
                }
                // template : '',
                // templateUrl: ''
            };
        }])
        .filter('firstComma',function(){
            return function(input){
                if( !input ){return '';}
                var index = input.indexOf(',');
                if(index === -1){
                    return input;
                }else{
                    return input.substring(0,index) + '...';
                }
            };
        })
        .filter('stageIdToValue',function(){
            return function(input,list){
                if( !input || !list ){return '';}
                var stage = _.find(list,{id:+input});
                if( input === '0'){return '无效';}
                return stage?stage.text:'';
            };
        })
        .filter('myDateFormat',function(){
            return function(input){
                if( !input){return '';}
                return input.substring(0,16);
            };
        });
});